<?php
  include("header.php");
  include("functions.php");
  
  if ($_SESSION['user_level'] != 1) {
      header("location: redirect.php");
  } else {
      $pageTitle = "Posts";
      $post_per_page = get_value_of('$post_per_page');
      $post_per_page = trim($post_per_page);
	  $topics_per_page = get_value_of('$topics_per_page');
      $topics_per_page = trim($topics_per_page);
      $allow_guest_posts = get_value_of('$allow_guest_posts');
      $allow_guest_posts = trim($allow_guest_posts);
      $allow_guest_posts = preg_replace("#[^0-9]#", "", $allow_guest_posts);
      if ($allow_guest_posts == "1") {
          $yes = "checked";
          $no = "";
      } else {
          $yes = "";
          $no = "checked";
      }
      
      $bad_word_filter = get_value_of('$bad_word_filter');
      $bad_word_filter = trim($bad_word_filter);
      $bad_word_filter = preg_replace("#[^0-9]#", "", $bad_word_filter);
      if ($bad_word_filter == "1") {
          $bwyes = "checked";
          $bwno = "";
      } else {
          $bwyes = "";
          $bwno = "checked";
      }
      
      $min_post_characters = get_value_of('$min_post_characters');
      $min_post_characters = trim($min_post_characters);
      $min_post_characters = preg_replace("#[^0-9]#", "", $min_post_characters);
      
      $max_post_characters = get_value_of('$max_post_characters');
      $max_post_characters = trim($max_post_characters);
      $max_post_characters = preg_replace("#[^0-9]#", "", $max_post_characters);
      
      $allow_smilies = get_value_of('$allow_smilies');
      $allow_smilies = trim($allow_smilies);
      $allow_smilies = preg_replace("#[^0-9]#", "", $allow_smilies);
      if ($allow_smilies == "1") {
          $asyes = "checked";
          $asno = "";
      } else {
          $asyes = "";
          $asno = "checked";
      }
      
      $allow_edit_post = get_value_of('$allow_edit_post');
      $allow_edit_post = trim($allow_edit_post);
      $allow_edit_post = preg_replace("#[^0-9]#", "", $allow_edit_post);
      if ($allow_edit_post == "1") {
          $aepyes = "checked";
          $aepno = "";
      } else {
          $aepyes = "";
          $aepno = "checked";
      }
      
      $allow_delete_post = get_value_of('$allow_delete_post');
      $allow_delete_post = trim($allow_delete_post);
      $allow_delete_post = preg_replace("#[^0-9]#", "", $allow_delete_post);
      if ($allow_delete_post == "1") {
          $adyes = "checked";
          $adno = "";
      } else {
          $adyes = "";
          $adno = "checked";
      }
      
      $hot_topics_post = get_value_of('$hot_topics_post');
      $hot_topics_post = trim($hot_topics_post);
      
      $show_last_edited_by = get_value_of('$show_last_edited_by');
      $show_last_edited_by = trim($show_last_edited_by);
      if ($show_last_edited_by == "1") {
          $ebyes = "checked";
          $ebno = "";
      } else {
          $ebyes = "";
          $ebno = "checked";
      }
      
      $default_wysiwyg = get_value_of('$default_wysiwyg');
      $default_wysiwyg = trim($default_wysiwyg);
      $default_wysiwyg = preg_replace("#[^0-9]#", "", $default_wysiwyg);
      if ($default_wysiwyg == "1") {
          $eyes = "checked";
          $eno = "";
      } else {
          $eyes = "";
          $eno = "checked";
      }
      
      $adsense_in_posts = get_value_of('$adsense_in_posts');
      $adsense_in_posts = trim($adsense_in_posts);
      $adsense_in_posts = preg_replace("#[^0-9]#", "", $adsense_in_posts);
      if ($adsense_in_posts == "1") {
          $adsyes = "checked";
          $adsno = "";
      } else {
          $adsyes = "";
          $adsno = "checked";
      }
      
	  $recent_posts_on_index = get_value_of('$recent_posts_on_index');
      $recent_posts_on_index = trim($recent_posts_on_index);
	  
	  $edit_post_time = get_value_of('$edit_post_time');
      $edit_post_time = trim($edit_post_time);
	  
	  $max_imagesize_in_posts = get_value_of('$max_imagesize_in_posts');
      $max_imagesize_in_posts = trim($max_imagesize_in_posts);
      $max_imagesize_in_posts = preg_replace("#[^0-9]#", "", $max_imagesize_in_posts);
	  
      if ($_SERVER['REQUEST_METHOD'] != 'POST') {
          $tags = array('{POSTS_PER_PAGE}', '{TOPICS_PER_PAGE}','{PP_VALUE}', '{TP_VALUE}','{TITLE}', '{GUEST_POSTS}', '{GP_VALUE}', '{GPYES}', '{GPNO}', '{BAD_WORD}', '{BWYES}', '{BWNO}', '{MIN_CHAR}', '{MIN_CHAR_VALUE}', '{SMILIES}', '{S_YES}', '{S_NO}', '{EDIT}', '{EDIT_YES}', '{EDIT_NO}', '{DELETE}', '{D_YES}', '{D_NO}', '{HOT_TOPIC}', '{HOT_VALUE}', '{EDITOR}', '{E_YES}', '{E_NO}', '{CONFIRM}', '{ADD}', '{EDIT_BY}', '{EB_YES}', '{EB_NO}', '{ADSENSE}', '{A_YES}', '{A_NO}', '{MAX_CHAR}', '{MAX_CHAR_VALUE}','{RECENT_POSTS}','{RP_VALUE}','{EDIT_TIME}','{EDIT_TIME_VALUE}','{MIN}','{IMG_SIZE}','{IMG_VALUE}');
		  
		  
          $data = array('Posts per page (min 3):', 'Topics per page',$post_per_page,  $topics_per_page,'Posts', 'Allow guest reply:', $allow_guest_posts, $yes, $no, 'Bad word filter:', $bwyes, $bwno, 'Min. post characters:', $min_post_characters, 'Allow smilies in posts:', $asyes, $asno, 'Allow edit posts:', $aepyes, $aepno, 'Allow delete posts:', $adyes, $adno, 'Hot topic threshold:', $hot_topics_post, 'WYSIWYG (default)', $eyes, $eno, 'Confirm', '[+] Add/edit', 'Show "Last edit by"', $ebyes, $ebno, 'Adsense in posts', $adsyes, $adsno, 'Max. post characters:', $max_post_characters,'Recent posts on index<br>(0 to disable)', $recent_posts_on_index,'Limit editing time:',$edit_post_time,'minutes','Max. image size in posts',$max_imagesize_in_posts);
          
          echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/posts.html"));
      } else {
          //post_per_page
          $ppp = mysql_real_escape_string($_POST['post_per_page']);
          if ($ppp <= 3) {
              $ppp = 3;
          }
		  $tpp = mysql_real_escape_string($_POST['topics_per_page']);
		  if ($tpp <= 3) {
              $tpp = 3;
          }

          $rpi = mysql_real_escape_string($_POST['recent_posts']);
		  if ($rpi >= 30) {
              $rpi = 30;
          }
		  write_value_of('$recent_posts_on_index', "$recent_posts_on_index", $rpi);
          write_value_of('$post_per_page', "$post_per_page", $ppp);
		  write_value_of('$topics_per_page', "$topics_per_page", $tpp);
          //allow_guest_posts
          write_value_of('$allow_guest_posts', "$allow_guest_posts", $_POST['guest_post']);
          //min_post_characters
          $mpc = $_POST['min_char'];
          if ($mpc <= 3) {
              $mpc = 3;
          }
          write_value_of('$min_post_characters', "$min_post_characters", $mpc);
          //min_post_characters
          $maxpc = $_POST['max_char'];
          if ($maxpc <= 200) {
              $maxpc = 200;
          }
          write_value_of('$max_post_characters', "$max_post_characters", $maxpc);
          //bad_word_filter
          write_value_of('$bad_word_filter', "$bad_word_filter", $_POST['bad_word']);
          //allow_smilies
          write_value_of('$allow_smilies', "$allow_smilies", $_POST['smilies']);
          //allow_edit_post
          write_value_of('$allow_edit_post', "$allow_edit_post", $_POST['edit']);
          //allow_delete_post
          write_value_of('$allow_delete_post', "$allow_delete_post", $_POST['delete']);
          //show_last_edited_by
          write_value_of('$show_last_edited_by', "$show_last_edited_by", $_POST['edit_by']);
          //hot_topics_post
          $ht = $_POST['hot_topic'];
          if ($ht <= 3) {
              $ht = 3;
          }
          write_value_of('$hot_topics_post', "$hot_topics_post", $ht);
          write_value_of('$max_imagesize_in_posts', "$max_imagesize_in_posts", $_POST['img_size']);
          //default_wysiwyg
          write_value_of('$default_wysiwyg', "$default_wysiwyg", $_POST['editor']);
          
          write_value_of('$adsense_in_posts', "$adsense_in_posts", $_POST['adsense']);
          
		  write_value_of('$edit_post_time', "$edit_post_time", $_POST['edit_time']);
          echo "<b>Information</b><hr><br /><div align='center'>Configuration updated successfully.<br /><br /><a href = 'posts.php'>Back to previous page</a></div>";
      }
      // Get all the page's HTML into a string
      $pageContents = ob_get_contents();
      // Wipe the buffer 
      ob_end_clean();
      echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
  }
?>
